﻿using AutoMapper;
using KayakMes.Common;
using KayakMes.Common.Dtos.ProcessDTO.Input;
using KayakMes.Common.Dtos.ProcessDTO.Output;
using KayakMes.Common.Result;
using KayakMes.ProcessStepUp.Domain.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.ProcessManagement.Application.Process
{
    public class ProcessStepUpService : IProcessStepUpService
    {
        private readonly BaseReposiotry<ProcessStepUpModel> _processStepUp;
        private readonly IMapper _mapper;

        public ProcessStepUpService(BaseReposiotry<ProcessStepUpModel> processStepUp, IMapper mapper)
        {
            _processStepUp = processStepUp;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增工序设置信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> Add(ProcessStepUpInput input)
        {
            var entity = _mapper.Map<ProcessStepUpModel>(input);
            entity.Id = YitIdHelper.NextId();
            // 生成4位随机数字
            Random random = new Random();
            // 格式化为4位数，不足4位前面补0
            entity.ProcessCode = "PROCESS" + random.Next(1000,9999);

            var res = _processStepUp.Add(entity);
            if (res>0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加工序设置成功",
                    Data = res
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加工序设置失败",
                    Data = 0
                };
            }
        }

        

        /// <summary>
        /// 获取工序设置信息列表（分页）
        /// </summary>
        /// <param name="input">查询条件，包含分页参数</param>
        /// <returns>分页结果</returns>
        public ApiPaging<ProcessStepUpOutput>GetProcessStepUpList(GetProcessStepUpInput input)
        {
            var query = _processStepUp.GetAll().Where(x=>!x.IsDeleted);
            if (input.IsEnabled != null)
            {
                query = query.Where(x=>x.IsEnabled == input.IsEnabled);
            }
            if (!string.IsNullOrEmpty(input.ProcessCode))
            {
                query = query.Where(x=>x.ProcessCode.Contains(input.ProcessCode));
            }
            if (!string.IsNullOrEmpty(input.ProcessName))
            {
                query = query.Where(x=>x.ProcessName.Contains(input.ProcessName));
            }
            
            // 计算总记录数
            int total = query.Count();
            
            // 获取分页参数
            int pageIndex = input.PageIndex;
            int pageSize = input.PageSize;
            
            // 执行分页查询
            var list = query.OrderBy(x => x.Id)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();
                
            // 使用AutoMapper映射为输出DTO
            var entity = _mapper.Map<List<ProcessStepUpOutput>>(list);

            return new ApiPaging<ProcessStepUpOutput>()
            {
                Total = total,
                PageIndex = pageIndex,
                PageSize = pageSize,
                Items = entity,
                Code = ApiEnums.Success,
                Message = "获取成功"
            };
        }

        /// <summary>
        /// 根据Id获取工序设置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<ProcessStepUpOutput> FindProcessStepUp(long id)
        {
            var query = _processStepUp.GetModel(id);

            var entity = _mapper.Map<ProcessStepUpOutput>(query);

            return new ApiResult<ProcessStepUpOutput>()
            {
                Code = ApiEnums.Success,
                Data = entity,
                Message = "查询成功"
            };
        }
        /// <summary>
        /// 根据ID删除工序设置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteProcessStepUp(long id)
        {
            var query = _processStepUp.GetModel(id);
            if (query != null && id > 0)
            {
                var res = _processStepUp.Delete(query);

                if (res>0)
                {
                    return new ApiResult<int>()
                        {
                            Code = ApiEnums.Success,
                            Data = res,
                            Message = "删除成功"
                        };
                }
                else
                {
                    return new ApiResult<int>()
                        {
                            Code = ApiEnums.Fail,
                            Data = res,
                            Message = "删除失败"
                        };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Data = -1,
                    Message = "找不到要删除的信息！"
                };
            }
        }

        /// <summary>
        /// 修改工序设置信息
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateProcessStepUp(ProcessStepUpOutput output)
        {
            var query = _processStepUp.GetModel(output.Id);

            if (query != null && output.Id>0)
            {
                var entity = _mapper.Map(output,query);
                
                var res = _processStepUp.Update(entity);

                if (res>0)
                {
                    return new ApiResult<int>
                        {
                            Code = ApiEnums.Success,
                            Message = "更新成功",
                            Data = res
                        };
                }
                else
                {
                    return new ApiResult<int>()
                        { Code = ApiEnums.Fail,
                        Data = res,
                        Message = "更新失败"
                        };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Data = -1,
                    Message = "找不到要修改的信息！"
                };
            }
        }
    }
}
